import {
  get,
  post
} from '../../../../request/index'

let timer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: "",
    codeMore: "",
    checked: false,
    imgIcon: "https://ryansystem-bucket.oss-cn-shenzhen.aliyuncs.com/upload/choice_icon.png",
    imgIcon1: "",
    isShow: false,
    moShow: false,
    show: true,
    show1: false,
    show2: false,
    
    loading: true,
    workTypeList: [],
    content: "",
    showOneButtonDialog: false,
    img: "",
    name: "",
    announceUrl: [], //班前要开会
    chargeUrl: [], //准备要充分
    regularUrl: [], //安全要确定
    emergencyUrl: [], //工作要检查
    checkUrl: [], //交班要清楚
    sort: ";",
    sortNumber: "",
    dataTime: "", //整改时间
    chanName: "", //责任人
    srt: "",
    dataText: "", //整改措施
    workStatus: "", //整改结果
    jsonType: [{
        label: "已整改",
        value: 1
      },
      {
        label: "未整改",
        value: 2
      }
    ],
    net: {
      url: '/app/memberinfo/getList',
      method: 'post',
      key: 'memberId',
      data: {
        mineId: true,
      },
      // value: 'memberId'
      value: 'nickName'
    },
    announcea: "",
    announceb: "",
    announcec: "",
    chargea: "",
    chargeb: "",
    chargec: "",
    regulara: "",
    regularb: "",
    regularc: "",
    regulard: "",
    regulare: "",
    emergencya: "",
    emergencyb: "",
    emergencyc: "",
    emergencyd: "",
    emergencye: "",
    checka: "",
    checkb: "",
    error: '',
    seachList: [],
   
    inputName: "",
    showMaster: false,
    baseButton: [{
      text: '确定'
    }],
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),
    numberSex: "",
    score: '#ffffff'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(query) {
    const {
      userid,
      status
    } = this.data.prolist
    console.log(userid,
      status);
    if (userid == undefined && status == undefined) {
      this.setData({
        codeMore: "1"
      })
    } else {
      if (userid == '' && status == "0") {
        
        this.setData({
          codeMore: "1"
        }) 
      } else { 
        this.setData({
          codeMore: "2"
        })
      }
    }



    this.Initial()
    this.getlogin()


  },
  getlogin() {
    let that = this
    wx.login({
      success(res) {
        if (res.code) {

          that.setData({
            code: res.code
          })

        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  onClose() {

    this.setData({
      show: false,
      loading: false,
      show1: false,
      show2: false
    });

  },
  getPhoneNumber(e) {
    console.log(e);
    let that = this;
    const {
      errMsg,
      iv,
      encryptedData
    } = e.detail


    if (errMsg == "getPhoneNumber:fail user deny") {
      return
    }

    wx.checkSession({
      success(res) {

        if (that.data.code) {

          get('/wx/wx/getUserInfoByCode', {
            code: that.data.code,
            encryptedData: encryptedData,
            iv: iv,
          }).then(res => {
            console.log(res);
            if (res.data == null) {
              that.getlogin()
              wx.showToast({
                title: '请到平台激活再使用',
                duration: 5000,
                icon: 'none',
                mask: true
              })
            } else {
              if (res.data.userid === "") {
                wx.showToast({
                  title: '您还未注册请先注册',
                  duration: 3000,
                  mask: true
                })
                wx.navigateTo({
                  url: `/pages/register/register?openid=${res.data.openid}`
                })
                return
              }


              if (!res.data.status) {
                wx.showToast({
                  title: '请重新登录',
                  duration: 3000,
                  mask: true
                })

                return
              } else {
                wx.setStorageSync('prolist', JSON.stringify(res.data))
                that.setData({
                  show: false,
                  loading: false,
                  show1: false,
                  show2: false
                });
              }



            }



          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      },

    })

  },
  onChange(event) {
    this.setData({
      result: event.detail,
    });
  },


  checkChange(event) {
    this.setData({
      checked: event.detail,
    });
    if (this.data.checked) {
      this.pageScrollToBottom()
      let list = this.data.workTypeList
      list.forEach((item, index) => {
        item.childrens = item.childrens.map((i, idx) => {
          return {
            name: i.name,
            regulation: i.regulation,
            sort: i.sort,
            meatlist: [{
              type: 1,
              typeText: '已落实',
              sort: i.sort,
              sureid: true,
              check: i.check,
              index: index,
              inx: idx,
            }, ]
          }


        })

      });
      this.setData({
        workTypeList: list,
        announcea: "1",
    announceb: "1",
    announcec: "1",
    chargea: "1",
    chargeb: "1",
    chargec: "1",
    regulara: "1",
    regularb: "1",
    regularc: "1",
    regulard: "1",
    regulare: "1",
    emergencya: "1",
    emergencyb: "1",
    emergencyc: "1",
    emergencyd: "1",
    emergencye: "1",
    checka: "1",
    checkb: "1",
      })

    } else {
      let list = this.data.workTypeList
      list.forEach((item, index) => {
        item.childrens = item.childrens.map((i, idx) => {
          return {
            name: i.name,
            regulation: i.regulation,
            sort: i.sort,
            meatlist: [{
              type: 1,
              typeText: '已落实',
              sort: i.sort,
              sureid: false,
              check: i.check,
              index: index,
              inx: idx,
            }, ]
          }


        })

      });
      this.setData({
        workTypeList: list,
        announcea: "",
    announceb: "",
    announcec: "",
    chargea: "",
    chargeb: "",
    chargec: "",
    regulara: "",
    regularb: "",
    regularc: "",
    regulard: "",
    regulare: "",
    emergencya: "",
    emergencyb: "",
    emergencyc: "",
    emergencyd: "",
    emergencye: "",
    checka: "",  
    checkb: "",
      })

    }


  },
  Initial() {

    post('/app/lineDirectornet/comeIn').then(res => {
      console.log(res);
      this.setData({
        numberSex: res.data
      })

    })

    post('/app/lineDirectornet/getList?currentPage=1&pageSize=100', {
      workType: 21
    }).then(res => {
      console.log(res);

      let list = res.data.list

      list.forEach((item, index) => {

        item.childrens = item.childrens.map((i, inx) => {
            return {

              name: i.name,
              regulation: i.regulation,
              sort: i.sort,

              meatlist: [{
                  type: 1,
                  typeText: '已落实',
                  sort: i.sort,
                  sureid: false,
                  check: i.check,
                  index: index,
                  inx: inx,
                },
                // {
                //   type: 2,
                //   typeText: '未落实',
                //   sort: i.sort,
                //   sureid: false,
                //   check: i.check,
                //   index: index,
                //   inx: inx,
                // },

              ]
            }

          }

        )

      })
      console.log(list);
      this.setData({
        workTypeList: res.data.list
      })
    })

  },
  tapOneDialogButton(e) {
    console.log(e);
    this.setData({
      showOneButtonDialog: true,
      content: e.currentTarget.dataset.item
    })
  },

  tapDialogButton(e) {

    this.setData({
      showOneButtonDialog: false
    })
  },

  onClose() {

    this.setData({
      show: false,
      loading: false,
      show1: false,
      show2: false
    });

  },
  tapOne(e) {
    console.log(e);
    let index = e.currentTarget.dataset.selectleft;
    let inx = e.currentTarget.dataset.inx
    let idx = e.currentTarget.dataset.index
    let sort = e.currentTarget.dataset.sort;
    let workTypeList = this.data.workTypeList
    let check = e.currentTarget.dataset.check
    workTypeList[idx].childrens[inx].meatlist[index].sureid = !workTypeList[idx].childrens[inx].meatlist[index].sureid
    this.setData({
      workTypeList: workTypeList,
    })

    if (sort == 1 && check == false) {
      this.setData({
        announcea: "1"
      })
    } else if (sort == 2 && check == false) {
      this.setData({
        announceb: "1"
      })
    } else if (sort == 3 && check == false) {
      this.setData({
        announcec: "1"
      })
    } else if (sort == 4 && check == false) {
      this.setData({
        chargea: "1"
      })
    } else if (sort == 5 && check == false) {
      this.setData({
        chargeb: "1"
      })
    } else if (sort == 6 && check == false) {
      this.setData({
        chargec: "1"
      })
    } else if (sort == 7 && check == false) {
      this.setData({
        regulara: "1"
      })
    } else if (sort == 8 && check == false) {
      this.setData({
        regularb: "1"
      })
    } else if (sort == 9 && check == false) {
      this.setData({
        regularc: "1"
      })
    } else if (sort == 10 && check == false) {
      this.setData({
        regulard: "1"
      })
    } else if (sort == 11 && check == false) {
      this.setData({
        regulare: "1"
      })
    } else if (sort == 12 && check == false) {
      this.setData({
        emergencya: "1"
      })
    } else if (sort == 13 && check == false) {
      this.setData({
        emergencyb: "1"
      })
    } else if (sort == 14 && check == false) {
      this.setData({
        emergencyc: "1"
      })
    } else if (sort == 15 && check == false) {
      this.setData({
        emergencyd: "1"
      })
    } else if (sort == 16 && check == false) {
      this.setData({
        emergencye: "1"
      })
    } else if (sort == 17 && check == false) {
      this.setData({
        checka: "1"
      })
    } else if (sort == 18 && check == false) {
      this.setData({
        checkb: "1"
      })
    }



    if (sort == 1 && check == true) {
      this.setData({
        announcea: ""
      })
    } else if (sort == 2 && check == true) {
      this.setData({
        announceb: ""
      })
    } else if (sort == 3 && check == true) {
      this.setData({
        announcec: ""
      })
    } else if (sort == 4 && check == true) {
      this.setData({
        chargea: ""
      })
    } else if (sort == 5 && check == true) {
      this.setData({
        chargeb: ""
      })
    } else if (sort == 6 && check == true) {
      this.setData({
        chargec: ""
      })
    } else if (sort == 7 && check == true) {
      this.setData({
        regulara: ""
      })
    } else if (sort == 8 && check == true) {
      this.setData({
        regularb: ""
      })
    } else if (sort == 9 && check == true) {
      this.setData({
        regularc: ""
      })
    } else if (sort == 10 && check == true) {
      this.setData({
        regulard: ""
      })
    } else if (sort == 11 && check == true) {
      this.setData({
        regulare: ""
      })
    } else if (sort == 12 && check == true) {
      this.setData({
        emergencya: ""
      })
    } else if (sort == 13 && check == true) {
      this.setData({
        emergencyb: ""
      })
    } else if (sort == 14 && check == true) {
      this.setData({
        emergencyc: ""
      })
    } else if (sort == 15 && check == true) {
      this.setData({
        emergencyd: ""
      })
    } else if (sort == 16 && check == true) {
      this.setData({
        emergencye: ""
      })
    } else if (sort == 17 && check == true) {
      this.setData({
        checka: ""
      })
    } else if (sort == 18 && check == true) {
      this.setData({
        checkb: ""
      })
    }


  },

  tapTwo(e) {

    let index = e.currentTarget.dataset.selectleft;
    let inx = e.currentTarget.dataset.inx
    let idx = e.currentTarget.dataset.index
    let sort = e.currentTarget.dataset.sort;
    let workTypeList = this.data.workTypeList
    workTypeList[idx].childrens[inx].meatlist[index].sureid = false




    this.setData({
      workTypeList: workTypeList,
      show2: true,
      sortNumber: sort,
      dataTime: '', //整改时间
      chanName: '', //责任人
      dataText: '', //整改措施
      workStatus: ''

    })

  },
  changea(e) {
    console.log(e);
    this.setData({
      chanName: e.detail
    })
  },
  changeb(e) {
    console.log(e);
    this.setData({
      dataTime: e.detail
    })
  },
  changec(e) {
    console.log(e);
    this.setData({
      dataText: e.detail
    })
  },
  changed(e) {
    console.log(e);
    this.setData({
      workStatus: e.detail
    })
  },

  submint() {

    let {
      announcea,
      announceb,
      announcec,
      chargea,
      chargeb,
      chargec,
      regulara,
      regularb,
      regularc,
      regulard,
      regulare,
      emergencya,
      emergencyb,
      emergencyc,
      emergencyd,
      emergencye,
      checka,
      checkb,
    } = this.data


    if (announcea != "" ||
      announceb != "" ||
      announcec != "" ||
      chargea != "" ||
      chargeb != "" ||
      chargec != "" ||
      regulara != "" ||
      regularb != "" ||
      regularc != "" ||
      regulard != "" ||
      regulare != "" ||
      emergencya != "" ||
      emergencyb != "" ||
      emergencyc != "" ||
      emergencyd != "" ||
      emergencye != "" ||
      checka != "" ||
      checkb != "" 
    ) {
     this.changeSub()
    } else {
      this.setData({
        error: '您还有未填项！'
      })
      return false
    }
  },


  onChangeout(e) {

    clearTimeout(timer);
    timer = setTimeout(async () => {
      let data = await post(`/app/mineinfo/getList`, {
        mineName: e.detail
      })

      this.setData({
        viewShow: true,
        seachList: data.data.list
      })
    }, 500);


  },



  tapButton() {
    this.setData({
      showMaster: false
    })
  },

  close() {
    this.setData({
      show2: false,
      show1: false,
    })
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    console.log(e)
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },

  //回到顶部
  goTop: function (e) { // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取容器高度，使页面滚动到容器底部
  pageScrollToBottom: function () {
    wx.createSelectorQuery().select('#j_page').boundingClientRect(function (rect) {
      // 使页面滚动到底部
      wx.pageScrollTo({
        scrollTop: rect.bottom
      })
    }).exec()
  },
  async changeSub() {

    let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'));
    let {
      announcea,
      announceb,
      announcec,
      chargea,
      chargeb,
      chargec,
      regulara,
      regularb,
      regularc,
      regulard,
      regulare,
      emergencya,
      emergencyb,
      emergencyc,
      emergencyd,
      emergencye,
      checka,
      checkb,
      announceUrl, //班前要开会
      chargeUrl, //准备要充分
      regularUrl, //安全要确定
      emergencyUrl, //工作要检查
      checkUrl,

        
    } = this.data

    let a = announceUrl.toString()
    let b = chargeUrl.toString()
    let c = regularUrl.toString()
    let d = emergencyUrl.toString()
    let e = checkUrl.toString()
    let res = await post(`/app/linecargo/insert`, {
      announcea: announcea,
      announceb: announceb,
      announcec: announcec,
      chargea: chargea,
      chargeb: chargeb,
      chargec: chargec,
      regulara: regulara,
      regularb: regularb,
      regularc: regularc,
      regulard: regulard,
      regulare: regulare,
      emergencya: emergencya,
      emergencyb: emergencyb,
      emergencyc: emergencyc,
      emergencyd: emergencyd,
      emergencye: emergencye,
      checka: checka,
      checkb: checkb,
      announceUrl: a, //班前要开会
      chargeUrl: b, //准备要充分
      regularUrl: c, //安全要确定
      emergencyUrl: d, //工作要检查
      checkUrl: e,
      mineId: prolist.mineId,
      createUser: prolist.userid
    })

    if (res.msg == "success") {
      this.setData({
        showMaster: true,
        checked:false
      })
      this.goTop()
      this.onLoad()
    }
  },
  // 未落实保存数据
  sbuit() {
    let {
      sortNumber,
      dataTime, //整改时间
      chanName, //责任人
      dataText, //整改措施
      workStatus, //整改结果
      sort //排序
    } = this.data
    if (chanName == '' || dataTime == '' || dataText == '') {

      this.setData({
        error: '请先填写再保存'
      })
      return false

    }
    switch (sortNumber) {
      case 1:
        this.setData({
          announcea: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 2:
        this.setData({
          announceb: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 3:
        this.setData({
          announcec: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 4:
        this.setData({
          chargea: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 5:
        this.setData({
          chargeb: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 6:
        this.setData({
          chargec: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 7:
        this.setData({
          regulara: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 8:
        this.setData({
          regularb: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 9:
        this.setData({
          regularc: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 10:
        this.setData({
          regulard: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 11:
        this.setData({
          regulare: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 12:
        this.setData({
          emergencya: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 13:
        this.setData({
          emergencyb: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 14:
        this.setData({
          emergencyc: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 15:
        this.setData({
          emergencyd: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 16:
        this.setData({
          emergencye: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 17:
        this.setData({
          checka: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 18:
        this.setData({
          checkb: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;

      default:
        break;
    }
  },

  upint(e) {

    console.log(e);

    this.setData({
      show1: true,
      name: e.currentTarget.dataset.name
    });
  },
  change(e) {
    let name = this.data.name
    switch (name) {
      case '库长有公示':
        let announceUrl = this.data.announceUrl

        announceUrl.push(e.detail)

        this.setData({
          announceUrl: announceUrl
        })

        break;
      case '责任有落实':
        let chargeUrl = this.data.chargeUrl

        chargeUrl.push(e.detail)

        this.setData({
          chargeUrl: chargeUrl
        })



        break;
      case '管控有措施':

        let regularUrl = this.data.regularUrl

        regularUrl.push(e.detail)

        this.setData({
          regularUrl: regularUrl
        })

        break;
      case '应急有预案':

        let emergencyUrl = this.data.emergencyUrl

        emergencyUrl.push(e.detail)

        this.setData({
          emergencyUrl: emergencyUrl
        })

        break;
      case '检查有记录':
        let checkUrl = this.data.checkUrl

        checkUrl.push(e.detail)

        this.setData({
          checkUrl: checkUrl
        })

        break;

      default:
        break;
    }



  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})